package com.tencent.wxcloudrun.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson2.JSONObject;
import com.tencent.wxcloudrun.constant.RedisKeyConstant;
import com.tencent.wxcloudrun.domain.TStoreProd;
import com.tencent.wxcloudrun.domain.TStoreProdAttr;
import com.tencent.wxcloudrun.domain.TStoreProdType;
import com.tencent.wxcloudrun.dto.StoreProdTypeListResDTO;
import com.tencent.wxcloudrun.exception.BusinessException;
import com.tencent.wxcloudrun.service.TStoreProdAttrService;
import com.tencent.wxcloudrun.service.TStoreProdService;
import com.tencent.wxcloudrun.service.TStoreProdTypeService;
import com.tencent.wxcloudrun.service.TUserService;
import com.tencent.wxcloudrun.util.RedisUtil;
import com.tencent.wxcloudrun.vo.request.store.*;
import com.tencent.wxcloudrun.vo.response.store.*;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author ZhaoZiYin
 * @Date 2025/4/22
 * @Description 商家管理端
 */
@Service
@Slf4j
public class StoreManager {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private TUserService userService;

    @Resource
    private TStoreProdTypeService storeProdTypeService;

    @Resource
    private TStoreProdService storeProdService;

    @Resource
    private TStoreProdAttrService storeProdAttrService;

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeAddResVo
     * @Description: 新增商品分类
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdTypeAddResVo addProdType(StoreProdTypeAddReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdTypeService.save(reqVo.build());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeDeleteResVo
     * @Description: 删除商品分类
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdTypeDeleteResVo deleteProdType(StoreProdTypeDeleteReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdTypeService.deleteProdType(reqVo.getUserId(), reqVo.getProdTypeId());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeListResVo
     * @Description: 查询商品分类
     */
    public StoreProdTypeListResVo listProdType(StoreProdTypeListReqVo reqVo) {
        StoreProdTypeListResVo resVo = new StoreProdTypeListResVo();
        List<TStoreProdType> prodTypeList = storeProdTypeService.listProdType();
        if (!CollectionUtils.isEmpty(prodTypeList)) {
            List<StoreProdTypeListResDTO> prodTypeDTOList = new ArrayList<>(prodTypeList.size());
            for (TStoreProdType prodType : prodTypeList) {
                StoreProdTypeListResDTO resDTO = new StoreProdTypeListResDTO();
                resDTO.setProdTypeId(prodType.getId());
                resDTO.setName(prodType.getName());
                resDTO.setMemo(prodType.getMemo());
                prodTypeDTOList.add(resDTO);
            }
            resVo.setProdTypeList(prodTypeDTOList);
        }
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeListResVo
     * @Description: 新增商品
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdAddResVo addProd(StoreProdAddReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        Long prodId = storeProdService.addProd(reqVo);
        storeProdAttrService.addProd(reqVo.getUserId(), prodId, reqVo.getProdAttrList());
        this.saveRedis4Prod(prodId);
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeListResVo
     * @Description: 更新商品
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdUpdateResVo updateProd(StoreProdUpdateReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdService.updateProd(reqVo);
        storeProdAttrService.deleteProd(reqVo.getProdId());
        storeProdAttrService.addProd(reqVo.getUserId(), reqVo.getProdId(), reqVo.getProdAttrList());
        this.saveRedis4Prod(reqVo.getProdId());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/23
     * @Params: [reqVo]
     * @Return: StoreProdPutOnResVo
     * @Description: 上架商品
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdPutOnResVo putOnProd(StoreProdPutOnReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdService.putOnProd(reqVo.getUserId(), reqVo.getProdId());
        this.saveRedis4Prod(reqVo.getProdId());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/23
     * @Params: [reqVo]
     * @Return: StoreProdTakeOffResVo
     * @Description: 下架商品
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdTakeOffResVo takeOffProd(StoreProdTakeOffReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdService.takeOffProd(reqVo.getUserId(), reqVo.getProdId());
        this.saveRedis4Prod(reqVo.getProdId());
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/27
     * @Params: [storeProd, prodAttrList]
     * @Return: void
     * @Description: 保存商品缓存
     */
    public void saveRedis4Prod(Long prodId) {
        TStoreProd prod = storeProdService.getById(prodId);
        List<TStoreProdAttr> prodAttrList = storeProdAttrService.listProd(prodId);
        String key = RedisKeyConstant.getStoreProdRedisKey(prodId);
        StoreProdQueryResVo resVo = new StoreProdQueryResVo();
        resVo.build(prod, prodAttrList);
        redisUtil.set(key, JSONObject.toJSONString(resVo));
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeListResVo
     * @Description: 删除商品
     */
    @Transactional(rollbackFor = Exception.class)
    public StoreProdDeleteResVo deleteProd(StoreProdDeleteReqVo reqVo) {
        userService.checkAdmin(reqVo.getUserId());
        storeProdService.deleteProd(reqVo.getUserId(), reqVo.getProdId());
        redisUtil.delete(RedisKeyConstant.getStoreProdRedisKey(reqVo.getProdId()));
        return null;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/23
     * @Params: [reqVo]
     * @Return: StoreProdQueryResVo
     * @Description: 查询商品详情
     */
    public StoreProdQueryResVo queryProd(StoreProdQueryReqVo reqVo) {
        StoreProdQueryResVo resVo = new StoreProdQueryResVo();
        String key = RedisKeyConstant.getStoreProdRedisKey(reqVo.getProdId());
        String resVoStr = redisUtil.get(key);
        if (StringUtils.hasLength(resVoStr)) {
            return JSONObject.parseObject(resVoStr, StoreProdQueryResVo.class);
        }
        TStoreProd prod = storeProdService.getById(reqVo.getProdId());
        if (Objects.isNull(prod)) {
            log.error("查询商品详情-商品不存在-prodId={}", reqVo.getProdId());
            throw new BusinessException("商品不存在");
        }
        List<TStoreProdAttr> prodAttrList = storeProdAttrService.listProd(reqVo.getProdId());
        resVo.build(prod, prodAttrList);
        redisUtil.set(key, JSONObject.toJSONString(resVo));
        return resVo;
    }

    /**
     * @Author: ZhaoZiYin
     * @Date: 2025/4/22
     * @Params: [reqVo]
     * @Return: StoreProdTypeListResVo
     * @Description: 查询商品
     */
    public StoreProdListResVo listProd(StoreProdListReqVo reqVo) {
        StoreProdListResVo resVo = new StoreProdListResVo();
        List<TStoreProd> prodList = storeProdService.listProd(reqVo);
        resVo.build(prodList);
        return resVo;
    }

}
